﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Typing_GUI
{
    public partial class frmManageLesson : Form
    {
        public int userID;
        public frmManageLesson()
        {
            InitializeComponent();
        }

        private void dgvLessonList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        int nCountlesson = 0;
        DataTable dt;
        void loadlesson()
        {
            dt = CLASS.CBaiTap.GetLessonUser(userID);
            nCountlesson = dt.Rows.Count;
            lblTotalLesson.Text = nCountlesson.ToString();
            dgvLessonList.DataSource = dt;
            dgvLessonList.Columns[0].Visible = false;
            dgvLessonList.Columns[5].Visible = false;
            dgvLessonList.Columns[6].Visible = false;
            dgvLessonList.Columns[7].Visible = false;
            dgvLessonList.Columns[8].Visible = false;
            dgvLessonList.Columns[9].Visible = false;
            dgvLessonList.Columns[2].MinimumWidth = 250;
            dgvLessonList.Columns[0].HeaderText = "No";
            dgvLessonList.Columns[1].HeaderText = "Lesson Name";
            dgvLessonList.Columns[2].HeaderText = "Content";
            dgvLessonList.Columns[3].HeaderText = "Descriptive";
            dgvLessonList.Columns[4].HeaderText = "Time";
            dgvLessonList.Columns[5].HeaderText = "Max Cose";
            dgvLessonList.Columns[6].HeaderText = "Level";
            dgvLessonList.Columns[7].HeaderText = "Type";
            dgvLessonList.Columns[8].HeaderText = "Send Lesson";
            dgvLessonList.Columns[9].HeaderText = "State";
        }
        private void frmManageLesson_Load(object sender, EventArgs e)
        {

            string un=CLASS.CNguoiDung.SetAvatar_Level(userID, pbAvatar, pbLevel);
            lblUsername.Text = un;
            loadlesson();


        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<int> mang_ma = new List<int>();
            if (dgvLessonList.SelectedRows.Count > 0)
            {
                DialogResult kq = MessageBox.Show("Do you want to delete this lesson?", "Notices", MessageBoxButtons.OKCancel);
                if (kq != DialogResult.Cancel)
                {
                    for (int i = dgvLessonList.SelectedRows.Count - 1; i >= 0; i--)
                    {
                        mang_ma.Add(int.Parse(dgvLessonList.SelectedRows[i].Cells[0].Value.ToString()));
                        CLASS.CBaiTap.DeleteLesson(int.Parse(dgvLessonList.SelectedRows[i].Cells[0].Value.ToString()));
                        dgvLessonList.Rows.Remove(dgvLessonList.SelectedRows[i]);
                        //nCountlesson--;
                    }
                    //lblTotalLesson.Text = nCountlesson.ToString();
                    loadlesson();
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dgvLessonList.SelectedRows.Count > 0)
            {
                CLASS.CBaiTap bt = new Typing_GUI.CLASS.CBaiTap();
                bt.Mabaitap = int.Parse(dgvLessonList.SelectedRows[0].Cells[0].Value.ToString());
                bt.Tenbaitap = dgvLessonList.SelectedRows[0].Cells[1].Value.ToString();
                bt.Noidung = dgvLessonList.SelectedRows[0].Cells[2].Value.ToString();
                bt.Mota=dgvLessonList.SelectedRows[0].Cells[3].Value.ToString();
                bt.Thoigian = int.Parse(dgvLessonList.SelectedRows[0].Cells[4].Value.ToString());
                bt.Nguoisoan = int.Parse(dgvLessonList.SelectedRows[0].Cells[8].Value.ToString());
                bt.TinhTrang = bool.Parse( dgvLessonList.SelectedRows[0].Cells[9].Value.ToString());
                frmEditTextLesson fm = new frmEditTextLesson();
                fm.editorID = bt.Nguoisoan;
                fm.LessonID = bt.Mabaitap;
                fm.adminUse = false;
                fm.Controls["txtMaxScore"].Visible = false;
                fm.Controls["combLevel"].Visible = false;
                fm.Controls["cobUser"].Visible = false;
                fm.Controls["label5"].Visible = false;
                fm.Controls["label7"].Visible = false;
                fm.Controls["label8"].Visible = false;
                fm.Controls["label10"].Visible = false;
                fm.Controls["chbState"].Visible = false;
                fm.Controls["label9"].Visible = false;
                //fm.Controls["txtLessonName"].Text = bt.Tenbaitap;
                //fm.Controls["txtTime"].Text = bt.Thoigian.ToString();
                //fm.Controls["txtDescription"].Text = bt.Mota;                  
                fm.ShowDialog();
                if (fm.updated == true)
                {
                    dgvLessonList.SelectedRows[0].Cells[3].Value = fm.lesson.Mota;
                    dgvLessonList.SelectedRows[0].Cells[1].Value = fm.lesson.Tenbaitap;
                    dgvLessonList.SelectedRows[0].Cells[2].Value = fm.lesson.Noidung;
                    dgvLessonList.SelectedRows[0].Cells[4].Value = fm.lesson.Thoigian.ToString();
                    dgvLessonList.SelectedRows[0].Cells[9].Value = fm.lesson.TinhTrang.ToString();
                }
                
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dt.Rows.Count < 10)
            {
                frmCreateLesson frm = new frmCreateLesson();
                frm.slbt = dt.Rows.Count;
                frm.userID = userID;
                frm.ShowDialog();
                loadlesson();
            }
            else
                MessageBox.Show("Sorry,List is full", "Noices");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void dgvLessonList_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnUpdate_Click(null, null);
        }
    }
}